# 定义登入名单
use_dict = {'common':{'denghui':'123123'},'control':{"root":'123456'}}
#VIP_use_dict = {'root':'123456'}
# 个人信息字典
information_dict ={}
# 一级登入
def enter(using_name,passwd):
 while True:
        if using_name in use_dict['common'] and passwd == use_dict['common'][using_name]:
            print("登入成功,进入普通用户页面！")
            return 1
        elif using_name in use_dict['control'] and passwd == use_dict['control'][using_name]:
            print("登入成功,进入管理员操作页面")
            return 2
        else:
            print(f"{'账号或者密码错误！':*^20}\n")
            return 0

# 普通用户
def common_interface(using_name):
 while True:
    print(f"{'':=^40}")
    print("{0:*^10}".format("普通用户界面"))
    input1 = input('*按r返回上一级菜单\n'
                   '*按m修改当前用户信息\n'
                   '*exit退出： ')
    if input1 == 'r':
        return 1
    elif input1 == 'm':
        input2 = input('修改当前爱好为：')
        information_dict[using_name] = input2
        print("修改成功！")
        continue
    elif input1 == 'exit':
        return 0
    else:
        print('输入无效，请重新输入！')
        continue
# 查看当前用户信息
def display(using_name):
    # 当前用户是普通用户
    if using_name in use_dict['common']:
        list1 = list(use_dict['common'].keys())
        for i in list1:
            if i == using_name:
                if not information_dict:
                    print("  姓名        密码        爱好")
                    print(f"{i}        {use_dict['control'][using_name]}        ")
                    return 1
                else:
                    print("  姓名        密码        爱好")
                    print(f"{i}        {use_dict['control'][using_name]}        {information_dict[using_name]}")
                    return 1
            else:
                continue
    # 当前用户是管理员
    else:
        list2 = list(use_dict['control'].keys())
        for j in list2:
            if j == using_name:
                if not information_dict:
                    print("  姓名        密码        爱好")
                    print(f"{j}        {use_dict['control'][using_name]}        ")
                    return 1
                else:
                    print("  姓名        密码        爱好")
                    print(f"{j}        {use_dict['control'][using_name]}        {information_dict[using_name]}")
                    return 1
            else:
                continue
# 搜索用户信息
def show(using_name):
    show_name = input("请输入你要搜索的用户名:")
    if show_name in use_dict['common']:
        print("该用户是普通用户")
        list1 = list(use_dict['common'].keys())
        for i in list1:
            if i == using_name:
                if not information_dict:
                    print("  姓名        密码        爱好")
                    print(f"{i}        {use_dict['common'][using_name]}        ")
                    break
                else:
                    print("  姓名        密码        爱好")
                    print(f"{i}        {use_dict['common'][using_name]}        {information_dict[using_name]}")
                    break
            else:
                continue
    elif show_name in use_dict['control']:
        print("该用户是管理员用户")
        list2 = list(use_dict['control'].keys())
        for j in list2:
            if j == using_name:
                if not information_dict:
                    print("  姓名        密码        爱好")
                    print(f"{j}        {use_dict['control'][using_name]}        ")
                    break
                else:
                    print("  姓名        密码        爱好")
                    print(f"{j}        {use_dict['control'][using_name]}        {information_dict[using_name]}")
                    break
            else:
                continue
    else:
      while True:
        print("查找不到你输入的用户名")
        input1 = input("按r返回上级菜单，按c继续查找：")
        if input1 == 'r':
            return 0
        elif input1 == 'c':
            return 1
        else:
            print("输入有误，请重新输入！")
            break


# 修改用户信息
def modification(using_name):
 while True:
    input_name = input("请输入要修改的用户名：")
    if input_name not in use_dict['common'] and input_name not in use_dict['control']:
        print("查找不到你要修改的用户名！")
        while True:
            flag = 0
            input11 = input("按r返回上一级菜单,按c重新输入：")
            if input11 == 'r':
               control_interface(using_name)
            elif input11 == 'c':
                flag = 1
                break
            else:
                print("输入有误，重新输入！")
                continue
        if flag == 1:
            continue
    else:
        input_passwd = input("请输入要修改的密码：")
        input_hobby = input("请输入要修改的爱好：")
        if input_name in use_dict['common']:
           use_dict['common'][input_name] = input_passwd
           information_dict[input_name] = input_hobby
           print(f"{'修改成功！':*^10}")
           control_interface(using_name)
           break
        else:
           use_dict['control'][input_name] = input_passwd
           information_dict[input_name] = input_hobby
           print(f"{'修改成功！':*^10}")
           control_interface(using_name)
           break
# 添加用户
def add():
 while True:
    add_name = input("请输入你要添加的用户名：")
    if add_name in use_dict['common'] or add_name in use_dict['control']:
        print("该用户已经存在，不能重复添加！")
        continue
    else:
        add_passwd = input("请输入此用户的密码：")
        add_hobby = input("请输入此用户的爱好：")
        while True:
            role = input("普通用户请输入‘common’，管理员‘control’：")
            if role == 'common':
                use_dict['common'][add_name] = add_passwd
                information_dict[add_name] = add_hobby
                print("添加成功！")
                return 1
            elif role == 'control':
                use_dict['control'][add_name] = add_passwd
                information_dict[add_name] =  add_hobby
                print("添加成功！")
                return 1
            else:
                print("输入有误，请重新输入！")
                continue
# 管理员界面
def control_interface(using_name):
 while True:
    print(f"{'':=^40}")
    input1 = input(f"{'按r返回上一级菜单':*^20}\n"
                   f"{'按l查看当前用户信息':*^20}\n"
                   f"{'按s搜索信息':*^20}\n"
                   f"{'按m修改用户信息':*^20}\n"
                   f"{'按d删除用户信息':*^20}\n"
                   f"{'按a添加用户':*^20}\n"
                   f"请输入：")
    # 返回上级菜单
    if input1 == 'r':
        return 0
    # 查看信息
    elif input1 == 'l':
        display(using_name)
    elif input1 == 's':
         re_show = show(using_name)
         if re_show == 1:
             show(using_name)
    # 修改用户信息
    elif input1 == 'm':
        modification(using_name)
    # 删除用户信息
    elif input1 == 'd':
        re_pop = pop_user(using_name)
        if re_pop == 1:
           control_interface(using_name)
    elif input1 == 'a':
        add()
    else:
        print("输入无效，请重新输入！")
# 删除用户信息
def pop_user(using_name):
 while True:
    input_name = input("请输入你要删除的用户名：")
    if input_name not in use_dict['common'] and input_name not in use_dict['control']:
        print("没有查找到你要删除的用户名")
        while True:
            flag = 0
            input_name2 = input("按r返回上一级菜单,按c重新输入：")
            if input_name2 == 'r':
                return 1
            elif input_name2 == 'c':
                flag = 1
                break
            else:
                print("输入有误，重新输入！")
                continue
        if flag == 1:
          continue
    else:
        if input_name in use_dict['common']:
            use_dict['common'].pop(input_name)
            if not information_dict:
                print("删除成功！")
                break
            else:
              information_dict.pop(input_name)
              print("删除成功！")
              break
        else:
            use_dict['control'].pop(input_name)
            if not information_dict:
                print("删除成功！")
                break
            else:
                information_dict.pop(input_name)
                print("删除成功！")
                break

# 主函数
while True:
    count = 0
    count += 0
    if count > 3:
        print("登入失败大于三次，程序退出！")
        break
    else:
        print(f"{'欢迎来到用户管理系统':#^50}")
        print("{0:*^20}".format('登入界面'))
        using_name = input("请输入你的用户名(exit退出)：")
        if using_name == 'exit':
            print("程序退出！")
            break
        else:
            passwd = input("请输入你的密码：")
            # 验证用户登入并且判断用户级别
            re_enter = enter(using_name, passwd)
            # re_enter返回值为1进入普通用户界面
            if re_enter == 1:
               re_comm = common_interface(using_name)
               # re_comm返回0，程序返回上一级菜单
               if re_comm == 1:
                   continue
               else:
                   print("程序结束!")
                   break
            # re_enter == 2 进入管理员操作页面
            elif re_enter == 2:
                re_control = control_interface(using_name)
                if re_control == 0:
                    continue
            else:
                print("请重新输入！")


